import requests
from lxml import etree

# 解析58二手房的相关数据
if __name__ == "__main__":
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36',
    }
    url = "https://wh.58.com/ershoufang/?PGTID=0d000000-0000-0f50-e617-cbbc05b26884&ClickID=1"
    r = requests.get(url=url, headers=headers)
    page_text = r.text
    # print(page_text)

    tree = etree.HTML(page_text)
    # //表示的是多个层级，可以表示从任意位置开始定位
    # /表示从根节点开始定位，表示的是一个层级
    li_list = tree.xpath('//ul[@class="house-list-wrap"]/li')
    data = []
    for li in li_list:
        # 解析标题
        title = li.xpath('.//div[@class="list-info"]/h2/a/text()')[0]
        print(title)
        data.append(title)

    # print(data)
